/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.ui.form.field.factory;

import com.rcnss.data.Structure;
import com.rcnss.data.User;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;

/**
 *
 * @author Guesmi
 */
public class UserFieldFactory extends DefaultFieldFactory {
    private IndexedContainer postContainer = new IndexedContainer();
    private IndexedContainer structContainer = new IndexedContainer();
    private int maxLen = 20;
    private String maxLenStr = maxLen+"em";

    @Override
    public Field createField(Item item, Object propertyId,
                Component uiContext) {
        Field f = super.createField(item, propertyId, uiContext);
        System.out.println("Field ("+propertyId+")");
        f.setWidth(maxLenStr);
        ((AbstractField)f).setImmediate(true);
        if("ID".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("I.D");
            tf.setEnabled(false);
        }else if ("POST".equals(propertyId))
        {
            initPostContainer();
            ComboBox c = new ComboBox("Poste", this.postContainer);
            c.setNullSelectionAllowed(false);
            c.setImmediate(true);
            c.setItemCaptionPropertyId("name");
            c.setFilteringMode(Filtering.FILTERINGMODE_STARTSWITH);
            c.setWidth(this.maxLenStr);
            return c;
        }
        else if ("matricule".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Matricule CNSS");
        } else if("PWD".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Mot de Passe");
            tf.setSecret(true);
        }
        else if("NOM".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Nom");
        }else if("login".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Compte");
        }else if("mail".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("E-mail");
            tf.addValidator(new EmailValidator("Mail non correcte"));
        }else if("codeStructure".equals(propertyId))
        {
            initStructContainer();
            ComboBox c = new ComboBox("Structure", this.structContainer);
            c.setNullSelectionAllowed(false);
            c.setImmediate(true);
            c.setItemCaptionPropertyId("name");
            c.setFilteringMode(Filtering.FILTERINGMODE_STARTSWITH);
            c.setWidth(this.maxLenStr);
            return c;
        }else if("prenom".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Prenom");
        }else if("telBureau".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Num Tel. Bureau");
        }else if("telMobile".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Num Tel. GSM");
        }else if("typeUser".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Droit Utilisateur");
        }
        return f;
    }

    private void initPostContainer()
    {
        this.postContainer.addContainerProperty("name", String.class, null);
        for(int i=0;i<User.Post.length;i++)
        {
            Item it = this.postContainer.addItem(i);
            if(it!=null)
            {
                it.getItemProperty("name").setValue(User.Post[i]);
                if(this.maxLen<User.Post[i].length())
                    this.maxLen = User.Post[i].length();
            }
        }
    }

    private void initStructContainer()
    {
        this.structContainer.addContainerProperty("name", String.class, null);
        for(Structure s : Structure.getAll())
        {
            Item it = this.structContainer.addItem(s.GetCODE());
            if(it!=null)
            {
                it.getItemProperty("name").setValue(s.GetLIB());
                if(this.maxLen<s.GetLIB().length())
                    this.maxLen = s.GetLIB().length();
            }
        }
    }
}

